//
// Created by jack on 2021/2/5.
//

#include "weilapb_defines.h"

#include <WL.Common.pb.h>
#include <pb.h>
#include <pb_decode.h>

#include "weilapb_datafuns.h"

#include "weilapb_commondec.h"
#include "weilapb_internal.h"

void weilapb_prepareUserInfoField(PBFieldData* pFieldList, WL_Common_UserInfo* pUserInfo, PBUserInfo* pInfo) {
    weilapb_initDecDataItem(
        &pFieldList[USER_NICK_INDEX], PB_DATA_TYPE_FIXED_SIZE_STRING, MAX_SHORT_STRING_LEN, pInfo->nick);
    weilapb_setDecCallback(&pFieldList[USER_NICK_INDEX], &pUserInfo->nick, PB_NULL);

    weilapb_initDecDataItem(
        &pFieldList[USER_NUMBER_INDEX], PB_DATA_TYPE_FIXED_SIZE_STRING, MAX_PHONE_NUM_LEN, pInfo->number);
    weilapb_setDecCallback(&pFieldList[USER_NUMBER_INDEX], &pUserInfo->number, PB_NULL);

    weilapb_initDecDataItem(&pFieldList[USER_COUNTRY_CODE_INDEX],
                            PB_DATA_TYPE_FIXED_SIZE_STRING,
                            MAX_SHORTEST_STRING_LEN,
                            pInfo->countryCode);
    weilapb_setDecCallback(&pFieldList[USER_COUNTRY_CODE_INDEX], &pUserInfo->countryCode, PB_NULL);

    weilapb_initDecDataItem(
        &pFieldList[USER_AVATAR_INDEX], PB_DATA_TYPE_FIXED_SIZE_STRING, MAX_URL_STRING_LEN, pInfo->avatar);
    weilapb_setDecCallback(&pFieldList[USER_AVATAR_INDEX], &pUserInfo->avatar, PB_NULL);

    weilapb_initDecDataItem(
        &pFieldList[USER_PHONE_INDEX], PB_DATA_TYPE_FIXED_SIZE_STRING, MAX_PHONE_NUM_LEN, pInfo->phone);
    weilapb_setDecCallback(&pFieldList[USER_PHONE_INDEX], &pUserInfo->phone, PB_NULL);

    weilapb_initDecDataItem(
        &pFieldList[USER_EMAIL_INDEX], PB_DATA_TYPE_FIXED_SIZE_STRING, MAX_URL_STRING_LEN, pInfo->email);
    weilapb_setDecCallback(&pFieldList[USER_EMAIL_INDEX], &pUserInfo->email, PB_NULL);
}

void* weilapb_decodeSubMsgUserInfo(pb_istream_t* stream, const pb_field_t* field) {
    WL_Common_UserInfo wlCommonUserInfo = WL_Common_UserInfo_init_zero;
    PBLinkItem*        pPbLinkItem      = PB_NULL;
    pPbLinkItem                         = (PBLinkItem*)PB_ALLOC(sizeof(PBLinkItem) + sizeof(PBUserInfo));
    if (pPbLinkItem) {
        PBUserInfo* pInfo;
        PBFieldData fieldData[USER_COUNT];
        pPbLinkItem->dataSize = sizeof(PBUserInfo);
        pInfo                 = (PBUserInfo*)pPbLinkItem->data;
        weilapb_prepareUserInfoField(fieldData, &wlCommonUserInfo, pInfo);
        if (pb_decode(stream, WL_Common_UserInfo_fields, &wlCommonUserInfo)) {
            if (wlCommonUserInfo.has_status) {
                pInfo->status = wlCommonUserInfo.status;
            }

            if (wlCommonUserInfo.has_userId) {
                pInfo->userId = wlCommonUserInfo.userId;
            }

            if (wlCommonUserInfo.has_created) {
                pInfo->createdTimestamp = wlCommonUserInfo.created;
            }

            if (wlCommonUserInfo.has_sex) {
                pInfo->sex = wlCommonUserInfo.sex;
            }
        } else {
            PB_FREE(pPbLinkItem);
            pPbLinkItem = PB_NULL;
        }
    }

    return pPbLinkItem;
}
